﻿using Coldairarrow.Business.IT;
using Coldairarrow.Business.PB;
using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.IT;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.TD;
using Coldairarrow.IBusiness;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.TD
{
    public partial class TD_AllocateBusiness : BaseBusiness<TD_Allocate>, ITD_AllocateBusiness, ITransientDependency
    {
        IServiceProvider _ServiceProvider { get; }
        readonly IOperator _operator;
        public TD_AllocateBusiness(IDbAccessor db, IServiceProvider svcProvider, IOperator @operator)
            : base(db)
        {
            _ServiceProvider = svcProvider;
            _operator = @operator;
        }

        public async Task<PageResult<TD_AllocateDTO>> GetDataListAsync(TD_AllocatePageInput input)
        {

            var search = input.Search;
            Expression<Func<TD_Allocate, Base_Department, Base_Department, Base_User, Base_User, Base_EnumItem, TD_AllocateDTO>> select = (a, b, c, d, e, f) => new TD_AllocateDTO
            {
                StorageName = b.Name,
                ToStorageName = c.Name,
                CreateUserName = d.RealName,
                AuditUserName = e.RealName,
                EnumName = f.Name
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.StorId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_Department>() on a.ToStorId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.CreatorId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    join e in Db.GetIQueryable<Base_User>() on a.AuditUserId equals e.Id into ae
                    from e in ae.DefaultIfEmpty()
                    join f in Db.GetIQueryable<Base_EnumItem>() on a.Type equals f.Code into af
                    from f in af.Where(u => u.EnumCode.Contains("AllocateType")).DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e, f);
            if (!search.Code.IsNullOrEmpty())
                q = q.Where(w => w.Code.Contains(search.Code) || w.RefCode.Contains(search.Code));
            if (!search.Type.IsNullOrEmpty())
                q = q.Where(w => w.Type == search.Type);
            if (!search.ToStorId.IsNullOrEmpty())
                q = q.Where(w => w.ToStorId == search.ToStorId);
            if (search.AllocateTimeStart.HasValue)
                q = q.Where(w => w.AllocateTime >= search.AllocateTimeStart.Value);
            if (search.AllocateTimeEnd.HasValue)
                q = q.Where(w => w.AllocateTime <= search.AllocateTimeEnd.Value);
            if (search.Status.HasValue)
                q = q.Where(w => w.Status == search.Status.Value);

            q =q.Where(d => _operator.Property.DeptIds.Contains(d.DeptId));

            return await q.GetPageResultAsync(input);

        }
        /// <summary>
        /// 根据调拨单ID进行反填数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TD_AllocateViewDTO> GetTheDataAsync(string id)
        {
            //TD_AllocateViewDTO
            TD_AllocateViewDTO pblist = new TD_AllocateViewDTO();
            TD_Allocate Datalist = await Db.GetIQueryable<TD_Allocate>().Where(x => x.Id == id).FirstOrDefaultAsync();
            //将父类对象赋值转换成子类对象
            pblist = AutoAdaptation.AutoCopy<TD_Allocate, TD_AllocateViewDTO>(Datalist);



            Expression<Func<TD_AllocateDetail, PB_Material,PB_Location,PB_Rack,PB_StorArea, PB_Location, PB_Rack, PB_StorArea, TD_AllocateDetailDTO>> select = (a,b,c,d,e,f,g,h) => new TD_AllocateDetailDTO
            {
                MaterialName = b.Name,//物料名称
                FromArea = e.Name,//原货区
                FromRack = d.Name, //原货架
                FromLocation = c.Name, //原货位
                ToArea = h.Name, // 目标货区
                ToRack = g.Name, //目标货架
                ToLocation = f.Name // 目标货位
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in Db.GetIQueryable<TD_AllocateDetail>().AsExpandable().Where(x => x.AllocateId == id)
                    join b in Db.GetIQueryable<PB_Material>() on a.MaterialId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_Location>() on a.FromLocalId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<PB_Rack>() on c.RackId equals d.Id into cd
                    from d in cd.DefaultIfEmpty()
                    join e in Db.GetIQueryable<PB_StorArea>() on c.AreaId equals e.Id into ce
                    from e in ce.DefaultIfEmpty()
                    join f in Db.GetIQueryable<PB_Location>() on a.ToLocalId equals f.Id into af
                    from f in af.DefaultIfEmpty()
                    join g in Db.GetIQueryable<PB_Rack>() on f.RackId equals g.Id into fg
                    from g in fg.DefaultIfEmpty()
                    join h in Db.GetIQueryable<PB_StorArea>() on f.AreaId equals h.Id into fh
                    from h in fh.DefaultIfEmpty()
                    select @select.Invoke(a, b, c, d, e, f, g, h);

            pblist.AllocateDetails = await q.ToListAsync();


            return pblist;





            //return await this.GetIQueryable()
            //    .Include(i => i.AllocateDetails)
            //        .ThenInclude(t => t.FromStor)
            //    .Include(i => i.AllocateDetails)
            //        .ThenInclude(t => t.FromLocal)
            //    .Include(i => i.AllocateDetails)
            //        .ThenInclude(t => t.ToLocal)
            //    .Include(i => i.AllocateDetails)
            //        .ThenInclude(t => t.Material)
            //    .Include(i => i.AllocateDetails)
            //        .ThenInclude(t => t.FromTray)
            //    .Include(i => i.AllocateDetails)
            //        .ThenInclude(t => t.FromZone)
            //    .SingleOrDefaultAsync(w => w.Id == id);
        }

        [DataAddLog(UserLogType.调拨管理, "Code", "调拨单")]
        [Transactional]
        public async Task AddDataAsync(TD_Allocate data)
        {
            if (data.Code.IsNullOrEmpty())
            {
                data.Code = DateTime.Now.ToString("yyyyMMddHHmmss");
            }
            TD_Allocate tD_Allocate = new TD_Allocate();
            tD_Allocate.Id = data.Id;
            tD_Allocate.AllocateTime = data.AllocateTime;
            tD_Allocate.Type = data.Type;
            tD_Allocate.StorId = data.StorId;
            tD_Allocate.ToStorId = data.ToStorId;
            tD_Allocate.RefCode = data.RefCode;
            tD_Allocate.AllocateNum = data.AllocateNum;
            tD_Allocate.Status = 0;
            tD_Allocate.Remarks = data.Remarks;
            tD_Allocate.CreateTime = DateTime.Now;
            tD_Allocate.CreatorId = _operator.UserId;
            tD_Allocate.Deleted = false;
            tD_Allocate.QrCode = h_Allocate + "," + data.Id;
            tD_Allocate.Code = data.Code;
            tD_Allocate.Amount = 0;   //总金额默认为0
            tD_Allocate.DeptId =  _operator?.Property?.DepartmentId; 
            await Db.InsertAsync<TD_Allocate>(tD_Allocate);


            List<TD_AllocateDetail> tD_AllocateDetaillist = new List<TD_AllocateDetail>();
            foreach (var item in data.AllocateDetails)
            {
                TD_AllocateDetail tD_AllocateDetail = new TD_AllocateDetail();
                tD_AllocateDetail.Id = item.Id;
                tD_AllocateDetail.AllocateId = item.AllocateId;
                tD_AllocateDetail.FromStorId = data.StorId;
                tD_AllocateDetail.ToStorId = data.ToStorId;
                tD_AllocateDetail.MaterialId = item.MaterialId;
                tD_AllocateDetail.AllocateNum = item.AllocateNum;
                
                tD_AllocateDetail.CreateTime = DateTime.Now;
                tD_AllocateDetail.CreatorId = _operator.UserId;
                tD_AllocateDetail.Deleted = false;
                
                tD_AllocateDetail.FromLocalId = item.FromLocalId; //原货位ID
                tD_AllocateDetail.ToLocalId = item.ToLocalId;   // 目标货位ID
                tD_AllocateDetail.Price = 0;  //单价默认为0
                tD_AllocateDetail.Amount = 0; //总金额默认为0
                // tD_AllocateDetail.BarCode = item.BarCode;
                // tD_AllocateDetail.BatchNo = item.BatchNo;
                //FromTrayId 原托盘ID
                //FromZoneId 原托盘分区ID
                tD_AllocateDetaillist.Add(tD_AllocateDetail);
            }
            await Db.InsertAsync<TD_AllocateDetail>(tD_AllocateDetaillist);
        }

        [DataEditLog(UserLogType.调拨管理, "Code", "调拨单")]
        [Transactional]
        public async Task UpdateDataAsync(TD_Allocate data)
        {
            await ExecuteSqlAsync("delete from TD_AllocateDetail  where AllocateId ='" + data.Id + "'");

            List<TD_AllocateDetail> tD_AllocateDetaillist = new List<TD_AllocateDetail>();
            foreach (var item in data.AllocateDetails)
            {
                TD_AllocateDetail tD_AllocateDetail = new TD_AllocateDetail();
                tD_AllocateDetail.Id = item.Id;
                tD_AllocateDetail.AllocateId = item.AllocateId;
                tD_AllocateDetail.FromStorId = item.FromStorId;
                tD_AllocateDetail.ToStorId = item.ToStorId;
                tD_AllocateDetail.MaterialId = item.MaterialId;
                tD_AllocateDetail.AllocateNum = item.AllocateNum;
               // tD_AllocateDetail.Price = item.Price;
               // tD_AllocateDetail.Amount = item.Price * item.AllocateNum;
                tD_AllocateDetail.CreateTime = DateTime.Now;
                tD_AllocateDetail.CreatorId = _operator.UserId;
                tD_AllocateDetail.Deleted = false;
                //tD_AllocateDetail.BarCode = item.BarCode; //条码
                //tD_AllocateDetail.BatchNo = item.BatchNo;  //批次号
                tD_AllocateDetail.FromLocalId = item.FromLocalId; //原货位ID
                tD_AllocateDetail.ToLocalId = item.ToLocalId;   // 目标货位ID
                //FromTrayId 原托盘ID
                //FromZoneId 原托盘分区ID
                tD_AllocateDetaillist.Add(tD_AllocateDetail);
            }
            await Db.InsertAsync<TD_AllocateDetail>(tD_AllocateDetaillist);


            TD_Allocate tD_Allocate = await Db.GetIQueryable<TD_Allocate>().Where(s => s.Id == data.Id).FirstOrDefaultAsync();
            tD_Allocate.AllocateTime = data.AllocateTime;
            tD_Allocate.Type = data.Type;
            tD_Allocate.StorId = data.StorId;
            tD_Allocate.ToStorId = data.ToStorId;
            tD_Allocate.RefCode = data.RefCode;
            //tD_Allocate.AllocateNum = data.AllocateDetails.Sum(s => s.AllocateNum);
            tD_Allocate.Status = 0;
            tD_Allocate.Remarks = data.Remarks;
            tD_Allocate.CreateTime = DateTime.Now;
            tD_Allocate.CreatorId = _operator.UserId;
            tD_Allocate.Deleted = false;
            tD_Allocate.QrCode = h_Allocate + "," + data.Id;
            tD_Allocate.Code = data.Code;
           // /tD_Allocate.Amount = data.Amount;
            List<string> props = GetFieldList(tD_Allocate);
            if (props.IsNullOrEmpty())
            {
                await UpdateAsync(tD_Allocate);
            }
            else
            {
                await UpdateAsync(tD_Allocate, props);
            }

            //var curDetail = data.AllocateDetails;
            //var listDetail = await Db.GetIQueryable<TD_AllocateDetail>().Where(w => w.AllocateId == data.Id).ToListAsync();

            //var curIds = curDetail.Select(s => s.Id).ToList();
            //var dbIds = listDetail.Select(s => s.Id).ToList();
            //var deleteIds = dbIds.Except(curIds).ToList();
            //var detailSvc = _ServiceProvider.GetRequiredService<ITD_AllocateDetailBusiness>();
            //if (deleteIds.Count > 0)
            //    await detailSvc.DeleteDataAsync(deleteIds);

            //var addIds = curIds.Except(dbIds).ToList();
            //if (addIds.Count > 0)
            //{
            //    var listAdds = curDetail.Where(w => addIds.Contains(w.Id)).ToList();
            //    await detailSvc.AddDataAsync(listAdds);
            //}

            //var updateIds = curIds.Except(addIds).ToList();
            //if (updateIds.Count > 0)
            //{
            //    var listUpdates = curDetail.Where(w => updateIds.Contains(w.Id)).ToList();
            //    await detailSvc.UpdateDataAsync(listUpdates);
            //}

            //data.AllocateNum = data.AllocateDetails.Sum(s => s.AllocateNum);
            //data.Amount = data.AllocateDetails.Sum(s => s.Amount);

            //await UpdateAsync(data);
        }


        [DataEditLog(UserLogType.调拨管理, "Id", "调拨单审批")]
        [Transactional]
        public async Task Approve(AuditDTO audit)
        {
            var now = DateTime.Now;
            var data = await this.GetTheDataAsync(audit.Id);
            var detail = data.AllocateDetails;
            //var dicMUnit = detail.GroupBy(s => new { s.Material.Id, s.Material.UnitGroupId }).Select(s => new { s.Key.Id, s.Key.UnitGroupId }).ToDictionary(k => k.Id, v => v.UnitGroupId);
            #region 出库入库代码
            ////调拨出库
            //{
            //    //出库单
            //    TD_OutStorage tD_OutStorage = new TD_OutStorage();
            //    tD_OutStorage.Id = IdHelper.GetId(); //主键
            //    tD_OutStorage.Code = DateTime.Now.ToString("yyyyMMddHHmmss");     //编号
            //    tD_OutStorage.StorageId = data.StorId; // 出货仓库
            //    tD_OutStorage.OutTime = DateTime.Now; //出库时间
            //    tD_OutStorage.OutType = "AllocateOut";
            //    tD_OutStorage.RefCode = data.Id;     //关联调拨单ID
            //    tD_OutStorage.OutNum = (double)data.AllocateNum;
            //    tD_OutStorage.TotalAmt = (double)data.Amount;
            //    tD_OutStorage.Status = 1;
            //    tD_OutStorage.AuditUserId = _operator.UserId;
            //    tD_OutStorage.AuditeTime = DateTime.Now;
            //    //tD_OutStorage.CusId = data.CusId;
            //    //tD_OutStorage.Address = data.Address;
            //    tD_OutStorage.Remarks = data.Remarks;
            //    tD_OutStorage.CreatorId = _operator.UserId;
            //    tD_OutStorage.CreateTime = DateTime.Now;
            //    tD_OutStorage.Deleted = false;
            //    await Db.InsertAsync<TD_OutStorage>(tD_OutStorage);
            //    //出库单明细
            //    List<TD_OutStorDetail> tD_OutStorDetaillist = new List<TD_OutStorDetail>();
            //    foreach (var item in detail)
            //    {
            //        var materdata = await Db.GetIQueryable<IT_LocalMaterial>().Where(x => x.MaterialId == item.MaterialId && x.StorId == item.FromStorId && x.LocalId == item.FromLocalId).FirstOrDefaultAsync();
            //        if (materdata == null)
            //        {
            //            TD_OutStorDetail tD_OutStorDetail = new TD_OutStorDetail();
            //            tD_OutStorDetail.Id = IdHelper.GetId(); ;
            //            tD_OutStorDetail.StorId = item.FromStorId;
            //            tD_OutStorDetail.OutStorId = tD_OutStorage.Id;  // 出库主表ID
            //            tD_OutStorDetail.LocalId = item.FromLocalId;
            //            //tD_OutStorDetail.AreaId = item.AreaId;
            //            //tD_OutStorDetail.RackId = item.RackId;
            //            tD_OutStorDetail.BarCode = item.BarCode;
            //            tD_OutStorDetail.MaterialId = item.MaterialId;
            //            //tD_OutStorDetail.BatchNo = item.BatchNo;
            //            tD_OutStorDetail.Price = item.Price;
            //            tD_OutStorDetail.TotalAmt = item.Amount;
            //            tD_OutStorDetail.LocalNum = item.LocalNum;
            //            tD_OutStorDetail.OutNum = item.AllocateNum;
            //            tD_OutStorDetail.CreateTime = DateTime.Now;
            //            tD_OutStorDetail.CreatorId = _operator.UserId;
            //            tD_OutStorDetail.Deleted = false;
            //            tD_OutStorDetaillist.Add(tD_OutStorDetail);
            //            await Db.InsertAsync<TD_OutStorDetail>(tD_OutStorDetaillist);
            //        }
            //        else
            //        {
            //            //存在修改
            //            string sqlMain = @"update IT_LocalMaterial set Nums_N=IFNULL(Nums_N,0)-" + item.AllocateNum + ",ScanNum=IFNULL(ScanNum,0)-" + item.AllocateNum + " where StorId='" + item.FromStorId + "' and LocalId='" + item.FromLocalId + "' and MaterialId='" + item.MaterialId + "';update PB_Material set CheckNum=IFNULL(CheckNum,0)-" + item.AllocateNum + ", ScanNum=IFNULL(ScanNum,0)-" + item.AllocateNum + " where Id='" + item.MaterialId + "'";
            //            await ExecuteSqlAsync(sqlMain);
            //        }

            //    }

            //    List<TD_OutStorRecord> tD_OutStorRecordlist = new List<TD_OutStorRecord>();
            //    var oUtlist = detail.GroupBy(s => new { s.MaterialId, s.Price, s.FromStorId }).Select(x => new { MaterialId = x.Key.MaterialId, Price = x.Key.Price, StorId = x.Key.FromStorId, Num = x.Sum(i => i.AllocateNum), TotalAmt = x.Sum(i => i.Amount) });
            //    foreach (var item in oUtlist)
            //    {
            //        TD_OutStorRecord tD_OutStorRecord = new TD_OutStorRecord();
            //        tD_OutStorRecord.Id = IdHelper.GetId();
            //        tD_OutStorRecord.StorId = item.StorId;
            //        tD_OutStorRecord.OutStorId = tD_OutStorage.Id; //出库主表ID
            //        tD_OutStorRecord.MaterialId = item.MaterialId;
            //        tD_OutStorRecord.Price = item.Price;
            //        tD_OutStorRecord.TotalAmt = item.TotalAmt;
            //        tD_OutStorRecord.OutNum = item.Num;
            //        tD_OutStorRecord.CreateTime = DateTime.Now;
            //        tD_OutStorRecord.CreatorId = _operator.UserId;
            //        tD_OutStorRecord.Deleted = false;
            //        tD_OutStorRecordlist.Add(tD_OutStorRecord);
            //    }
            //    await Db.InsertAsync<TD_OutStorRecord>(tD_OutStorRecordlist);

            //}
            ////调拨入库
            //{
            //    //入库单
            //    TD_InStorage tD_InStorage = new TD_InStorage();
            //    tD_InStorage.Id = IdHelper.GetId(); //主键
            //    tD_InStorage.Code = DateTime.Now.ToString("yyyyMMddHHmmss");     //编号
            //    tD_InStorage.StorId = data.ToStorId; // 入库仓库
            //    tD_InStorage.InStorTime = DateTime.Now; //入库时间
            //    tD_InStorage.InType = "AllocateIn";
            //    tD_InStorage.RefCode = data.Id;     //关联调拨单ID
            //    tD_InStorage.TotalNum = (double)data.AllocateNum;
            //    tD_InStorage.TotalAmt = (double)data.Amount;
            //    tD_InStorage.Status = 1;
            //    tD_InStorage.AuditUserId = _operator.UserId;
            //    tD_InStorage.AuditeTime = DateTime.Now;
            //    //tD_InStorage.CusId = data.CusId;
            //    //tD_InStorage.Address = data.Address;
            //    tD_InStorage.Remarks = data.Remarks;
            //    tD_InStorage.CreatorId = _operator.UserId;
            //    tD_InStorage.CreateTime = DateTime.Now;
            //    tD_InStorage.Deleted = false;
            //    await Db.InsertAsync<TD_InStorage>(tD_InStorage);
            //    //入库单明细
            //    List<TD_InStorDetail> tD_InStorDetaillist = new List<TD_InStorDetail>();
            //    foreach (var item in detail)
            //    {
            //        var materdata = await Db.GetIQueryable<IT_LocalMaterial>().Where(x => x.MaterialId == item.MaterialId && x.StorId == item.ToStorId && x.LocalId == item.ToLocalId).FirstOrDefaultAsync();
            //        if (materdata == null)
            //        {
            //            TD_InStorDetail tD_InStorDetail = new TD_InStorDetail();
            //            tD_InStorDetail.Id = IdHelper.GetId(); ;
            //            tD_InStorDetail.StorId = item.ToStorId;
            //            tD_InStorDetail.InStorId = tD_InStorage.Id;  // 入库主表ID
            //            tD_InStorDetail.LocalId = item.ToLocalId;
            //            tD_InStorDetail.BarCode = item.BarCode;
            //            tD_InStorDetail.MaterialId = item.MaterialId;
            //            tD_InStorDetail.Price = item.Price;    //单价
            //            tD_InStorDetail.Num = item.AllocateNum;  // 入库数量
            //            tD_InStorDetail.TotalAmt = item.Amount; //总额
            //            tD_InStorDetail.CreateTime = DateTime.Now;
            //            tD_InStorDetail.CreatorId = _operator.UserId;
            //            tD_InStorDetail.Deleted = false;
            //            tD_InStorDetaillist.Add(tD_InStorDetail);
            //            //tD_InStorDetail.AreaId = item.AreaId;
            //            //tD_InStorDetail.RackId = item.RackId;
            //            //tD_InStorDetail.BatchNo = item.BatchNo;
            //            await Db.InsertAsync<TD_InStorDetail>(tD_InStorDetaillist);
            //        }
            //        else
            //        {
            //            //存在修改
            //            string sqlMain = @"update IT_LocalMaterial set Nums_N=IFNULL(Nums_N,0)+" + item.AllocateNum + ",ScanNum=IFNULL(ScanNum,0)+" + item.AllocateNum + " where StorId='" + item.ToStorId + "' and LocalId='" + item.ToLocalId + "' and MaterialId='" + item.MaterialId + "';update PB_Material set CheckNum=IFNULL(CheckNum,0)+" + item.AllocateNum + ", ScanNum=IFNULL(ScanNum,0)+" + item.AllocateNum + " where Id='" + item.MaterialId + "'";
            //            await ExecuteSqlAsync(sqlMain);
            //        }
            //        }

            //    List<TD_InStorRecord> tD_InStorRecordlist = new List<TD_InStorRecord>();
            //    var iNlist = detail.GroupBy(s => new { s.MaterialId, s.Price, s.ToStorId }).Select(x => new { MaterialId = x.Key.MaterialId, Price = x.Key.Price, StorId = x.Key.ToStorId, Num = x.Sum(i => i.AllocateNum), TotalAmt = x.Sum(i => i.Amount) });
            //    foreach (var item in iNlist)
            //    {
            //        TD_InStorRecord tD_InStorRecord = new TD_InStorRecord();
            //        tD_InStorRecord.Id = IdHelper.GetId();
            //        tD_InStorRecord.StorId = item.StorId;
            //        tD_InStorRecord.InStorId = tD_InStorage.Id; //入库主表ID
            //        tD_InStorRecord.MaterialId = item.MaterialId;
            //        tD_InStorRecord.Price = item.Price;
            //        tD_InStorRecord.TotalAmt = item.TotalAmt;
            //        tD_InStorRecord.Num = item.Num;
            //        tD_InStorRecord.CreateTime = DateTime.Now;
            //        tD_InStorRecord.CreatorId = _operator.UserId;
            //        tD_InStorRecord.Deleted = false;
            //        tD_InStorRecordlist.Add(tD_InStorRecord);
            //    }
            //    await Db.InsertAsync<TD_InStorRecord>(tD_InStorRecordlist);

            //}
            #endregion

            // 修改主数据状态
            {
                data.Status = 1;
                //data.ToLocalId = defaultAllocateLocation.Id;
                data.AuditeTime = audit.AuditTime;
                data.AuditUserId = audit.AuditUserId;
                await UpdateAsync(data);
            }
        }

        [DataEditLog(UserLogType.调拨管理, "Id", "调拨单驳回")]
        public async Task Reject(AuditDTO audit)
        {
            var data = await this.GetEntityAsync(audit.Id);
            // 修改主数据状态
            {
                data.Status = 2;
                data.AuditeTime = audit.AuditTime;
                data.AuditUserId = audit.AuditUserId;
                await UpdateAsync(data);
            }
        }

        [DataEditLog(UserLogType.调拨管理, "Id", "开始作业")]
        public async Task StartWork(AuditDTO audit){
            var data = await this.GetEntityAsync(audit.Id);
            // 修改主数据状态
            {
                data.Status = 3;
                await UpdateAsync(data);
            }
        }
    }
}